package com.punch.user.controller;

import com.punch.common.dto.user.student.StudentDTO;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.query.user.student.StudentForAppQueryInfo;
import com.punch.common.query.user.student.StudentQueryInfo;
import com.punch.common.vo.user.student.StudentPageVO;
import com.punch.common.vo.user.student.StudentVO;
import com.punch.user.model.SysStudent;
import com.punch.user.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 学生信息表
 *
 * @author tanchangguo
 * @email 17607195327@163.com
 * @date 2020-07-21 19:10:02
 */
@RestController
@Api(tags = "学生信息表管理")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 学生列表
     */
    @GetMapping(value = "/students", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "学生列表")
    public Result list(@ModelAttribute StudentQueryInfo queryInfo) {
        return Result.succeed(studentService.queryPage(queryInfo));
    }

    /**
     * 远程调用学生列表
     *
     * @param schoolId    学校id
     * @param studentName 学生姓名
     * @return List<StudentPageVO>
     */
    @GetMapping(value = "/students/feign/school/{schoolId}", produces = "application/json; charset=utf-8")
    public PageInfo<StudentPageVO> feignStudentList(@PathVariable Long schoolId,
                                                    @RequestParam("studentName") String studentName,
                                                    @RequestParam(value = "currPage", required = false) Integer currPage,
                                                    @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return studentService.feignStudentList(schoolId, studentName, currPage, pageSize);
    }

    /**
     * 新增学生信息
     */
    @PostMapping(value = "/student", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增学生信息")
    public Result saveStudent(@Valid @RequestBody StudentDTO studentDTO) {
        return Result.succeed(studentService.saveStudent(studentDTO));
    }

    /**
     * 修改学生信息
     */
    @PutMapping("/student")
    @ApiOperation(value = "修改学生信息")
    public Result updateStudent(@RequestBody StudentDTO studentDTO) {
        studentService.updateStudent(studentDTO);
        return Result.succeed("修改成功");
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/student/{studentId}")
    @ApiOperation(value = "删除学生")
    public Result deleteStudent(@PathVariable("studentId") Long studentId) {
        studentService.deleteStudent(studentId);
        return Result.succeed("删除成功");
    }

    /**
     * 查看学生详情
     */
    @GetMapping("/student/{studentId}")
    @ApiOperation(value = "查看学生详情", response = StudentVO.class)
    public Result detail(@PathVariable("studentId") Long studentId) {
        SysStudent sysStudent = studentService.getById(studentId);
        StudentVO vo = new StudentVO();
        BeanUtils.copyProperties(sysStudent, vo);
        vo.setStudentId(studentId);
        return Result.succeed(vo);
    }

    /**
     * 条件搜索学生信息（小程序端）
     *
     * @author xiaol
     * @since 20207.29 23:10
     */
    @GetMapping("/student/app")
    @ApiOperation(value = "条件搜索学生信息(家长添加学生搜索列表——小程序端)")
    public Result list(@ModelAttribute StudentForAppQueryInfo studentForAppQueryInfo) {
        return Result.succeed(studentService.queryPageForApp(studentForAppQueryInfo));
    }

    /**
     * 学生启用/停用
     */
    @PutMapping("/student/enable/{studentId}")
    @ApiOperation(value = "学生启用/停用")
    public Result enableStudent(@PathVariable Long studentId) {
        studentService.enableStudent(studentId);
        return Result.succeed("修改成功");
    }

//    /**
//     * 服务调用根据id查询学生信息
//     *
//     * @param studentId 学生id
//     * @return StudentVO
//     */
//    @GetMapping(value = "/student/feign/{studentId}", produces = "application/json; charset=utf-8")
//    public StudentVO getFeignStudentById(@PathVariable("studentId") Long studentId) {
//        SysStudent sysStudent = studentService.getById(studentId);
//        StudentVO retStudent = new StudentVO();
//        BeanUtils.copyProperties(sysStudent, retStudent);
//        retStudent.setStudentId(studentId);
//        retStudent.setSchoolId(sysStudent.getCreateId());
//        return retStudent;
//    }

    /**
     * 学生导入
     *
     * @param file 文件
     * @return string
     */
    @PostMapping(value = "/student/import", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "学生导入", notes = "学生导入", produces = "application/json")
    public Result importStudent(@RequestParam("file") MultipartFile file) {
        studentService.importStudent(file);
        return Result.succeed("导入成功");
    }

    /**
     * 学校年级下学生报班信息导出
     *
     * @param schoolName 学校名称
     * @param gradeYear  年级
     * @param gradeClass 班级
     * @param className  课程名称
     */
    @GetMapping(value = "/student/class/export", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "学校年级下学生报班信息导出", notes = "学校年级下学生报班信息导出", produces = "application/json")
    public void exportClassStudent(@RequestParam("schoolName") String schoolName,
                                   @RequestParam("gradeYear") Integer gradeYear,
                                   @RequestParam("gradeClass") String gradeClass,
                                   @RequestParam(value = "className", required = false) String className,
                                   HttpServletResponse response) throws IOException {
        studentService.exportClassStudent(schoolName.trim(), gradeYear, gradeClass.trim(), className, response);
    }

    @GetMapping(value = "/student/handlerStudentArea", produces = "application/json; charset=utf-8")
    public void handlerStudentArea() {
        studentService.handlerStudentArea();
    }
}
